package com.itheima.exerciseTest.method10_19.method11;

import java.text.Collator;
import java.util.Locale;

/*
    分别用Comparable和Comparator两个接口对下列四位同学的成绩做降序排序，
    如果成绩一样，那在成绩排序的基础上按照年龄由小到大排序。
    姓名（String）	年龄（int）	分数（float）
    liusan	        20	        90.0F
    lisi		    22	        90.0F
    wangwu		    20	        99.0F
    sunliu		    22	        100.0F
 */
public class Student implements Comparable<Student> {
    private String name; // 姓名
    private int age; // 年龄
    private float grade; // 分数

    public Student() {
    }

    public Student(String name, int age, float grade) {
        this.name = name;
        this.age = age;
        this.grade = grade;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public float getGrade() {
        return grade;
    }

    public void setGrade(float grade) {
        this.grade = grade;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", grade=" + grade +
                '}';
    }

    @Override
    public int compareTo(Student s) {
        // 中文比较器
        Collator ins = Collator.getInstance(Locale.CHINESE);
        // 比较成绩
        int result = Float.compare(this.grade, s.grade);
        // 比较年龄
        result = result == 0 ? this.age - s.age : -result;
        if(result != 0){
            return result;
        }
        // 比较名字
        if (result == 0) {
            char[] chars = this.name.toCharArray();
            boolean boo = false;
            // 判断传入字符是中文字符还是英文子符
            for (int i = 0; i < chars.length; i++) {
                if (chars[i] >= 'A' && chars[i] <= 'Z') {
                    boo = true;
                    break;
                } else if (chars[i] >= 'a' && chars[i] <= 'z') {
                    boo = true;
                    break;
                }
            }
            if (boo == true) {
                result = this.name.compareTo(s.name);
                return result;
            } else {
                result = ins.compare(this.name, s.name);
            }
        }
        return result;
    }
}
